var api = require('./api/api');
var utils = require('./utils');

var log4js = require("log4js");
var config = require('./config.json');
const BigNumber = require('bignumber.js');

log4js.configure(config);
const logger = log4js.getLogger('server');

exports.route = function route(data) {
	
	if (typeof(data) !== 'string') {
		return utils.JSONError(logger, 10001, "Data is not in string format");
	}

	try {
        var dataObj = JSON.parse(data);
	}
	catch (e) {
        return utils.JSONError(logger, 10005, "JSON parsing failed");
    }

    if (typeof(dataObj.version) == "undefined" || dataObj.version.toString().trim() != config.api_version) {
        return utils.JSONError(logger, 10007, "API request version does not match");
    }

    try {
        return api.handleMsg(dataObj);

        // if (dataObj.method.toString().trim() === "signTx") {
        //     return api.handleMsg(dataObj);
        // } else if (dataObj.method.toString().trim() === "generateMnemonic") {
        //     return api.handleMsg(dataObj);
        // } else if (dataObj.method.toString().trim() === "generateAddress") {
        //     return api.handleMsg(dataObj);
        // } else {
        //     return utils.JSONError(logger, 10004, "Not found method");
        // }
    }
    catch (err) {
	    if (err.toString() == "Parameter error") {
	        return utils.JSONError(logger, 10001, "Parameter error.");
        }
        return utils.JSONError(logger, 10002, err.toString());
    }
};
